package in.muton.edushare.academics.alerts;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.messaging.api.SystemMessage;
import in.muton.edushare.grading.panels.studentGrades.GradingUtil.ItemGradeHelper;
import in.muton.edushare.wicket.models.CayenneList;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;

import com.edushare.common.mailer.MailBase;
import com.edushare.common.mailer.MailStringLoader;
import com.edushare.common.vo.Users;

public class ComponentAlert {
	private ItemGradeHelper item;

	public ComponentAlert(ItemGradeHelper item) {
		this.item = item;
	}

	public void sendAlerts() {
		String marks = item.marks + " of "
				+ item.gradingItem.getObject().getMaxMarks();
		HashMap<String, String> replaceMap = new HashMap<String, String>();
		replaceMap.put("ITEM_NAME", item.gradingItem.getObject()
				.getGradingItemName());
		replaceMap.put("ITEM_GRADE", marks);
		String message = MailStringLoader.templateParser(
				componentMarksAlertString, replaceMap);
		CayenneList<Users> cl = new CayenneList<Users>(Arrays.asList(item.user
				.getObject()));

		new SystemMessage(EduShareSession.get().getSessionORMInterface(),
				EduShareSession.get().getLoggedInUser(), item.gradingItem
						.getObject().getCourseInstance(), message, cl).send();
	}

	public static String componentMarksAlertString = "";
	public static String subjectMarksAlertString = "";
	static {
		try {
			componentMarksAlertString = MailBase
					.readInputStream(ComponentAlert.class
							.getResourceAsStream("ComponentAlert.msg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
